﻿using System;
using System.Collections.Generic;
using System.Text;

namespace BigfootMVC.Helpers
{
    /// <summary>
    /// This is a conditional render helper. It supports outputing content based on the conditions specified
    /// </summary>
    public class RenderIfBuilder
    {

        bool _condition;
        string _truecontent;
        string _elsecontent;
        bool _containselsecontent;

        /// <summary>
        /// Render the content if the condition is true
        /// </summary>
        /// <param name="condition">The condition to define weather to render the content</param>
        /// <param name="truecontent">The content to render</param>
        public RenderIfBuilder(bool condition, string truecontent)
        {
            _condition = condition;
            _truecontent = truecontent;
        }

        /// <summary>
        /// Render the content if the condition is true using String.Format
        /// </summary>
        /// <param name="condition">The condition to define weather to render the content</param>
        /// <param name="trueformat">The content to use in the String.Format</param>
        /// <param name="values">The values to use for the String.Format</param>
        public RenderIfBuilder(bool condition, string trueformat, params string[] values)
        {
            _condition = condition;
            _truecontent = string.Format(trueformat, values);
        }

        /// <summary>
        /// Renders this content if the condition is false
        /// </summary>
        /// <param name="elsecontent">The content to render</param>
        /// <returns>Returns the builder to aid in the fluid</returns>
        public RenderIfBuilder Else(string elsecontent)
        {
            _containselsecontent = true;
            _elsecontent = elsecontent;
            return this;
        }

        /// <summary>
        /// Renders this content if the condition is false using String.Format
        /// </summary>
        /// <param name="elseformat">The content to render</param>
        /// <param name="params">The parameters to use in the String.Format</param>
        /// <returns>Returns the builder to aid in the fluid</returns>
        public RenderIfBuilder Else(string elseformat, params string[] @params)
        {
            _containselsecontent = true;
            _elsecontent = string.Format(elseformat, @params);
            return this;
        }

        /// <summary>
        /// Renders the corrent content
        /// </summary>
        public override string ToString()
        {
            if (_condition) { return _truecontent; }
            return _containselsecontent ? _elsecontent : "";
        }
    }

}
